package itheima_edu_info_manager.com.itheima.edu.info.manager.dao;

import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Student;

public class StudentDao extends BaseStudentDao{
    //将数组变为成员变量，并用static进行修饰（将此数组变成公共数组）
    private static Student[] stus = new Student[5];

    static {
        Student student = new Student("heima001","张三","19","1997-11-11");
        Student student1 = new Student("heima002","李四","22","1990-11-11");

        stus[0] = student;
        stus[1] = student1;
    }

    //添加学生
    public boolean addStudent(Student stu) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("添加失败！");
            return false;
        } else {
            stus[index] = stu;
            return true;
        }
    }

    //查看学生
    public Student[] findAllStudent() {
        return stus;
    }

    //删除学生
    public void deleteStudentById(String delid) {
        int index = getIndex(delid);
        stus[index] = null;
    }

    //获取对应ID的索引编号
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student st = stus[i];
            if (st != null && st.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改学生
    public void upDateStudentById(String id, Student stu) {
        int index = getIndex(id);
        stus[index] = stu;
    }
}
